/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Sicovi.DAO;

import Com.Sicovi.Entidad.Empresa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author jotace
 */
public class EmpresaDAO {
    private EntityManagerFactory emf;
    private EntityManager em;
    
    public EmpresaDAO(){
        emf=Persistence.createEntityManagerFactory("SICOVIPU");
        em=emf.createEntityManager();
    }
    
    public void CloseEntityManager(){
        em.close();
    }
    public List<Empresa> getListarEmpresas(){
     Query q = em.createQuery("select e from Empresa e WHERE e.estado=1");
       return q.getResultList();
    }
    
    public boolean InsertarEmpresa(Empresa emp){
        boolean b=false;
        try{
            em.getTransaction().begin();
            em.persist(emp);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b; 
    }
    
   
     public List<Empresa> BuscarEmpresa(String nombre){
        Query q = em.createQuery("select e from Empresa e where e.razonSocial='"+nombre+"'");
        return q.getResultList();
    }     
         
     public boolean  eliminaEmpresa(int idEmpresa){
      boolean b=false;
        try{
            em.getTransaction().begin();
            Query q = em.createQuery("UPDATE Empresa e SET e.estado=0 WHERE e.idEmpresa="+idEmpresa);
            q.executeUpdate();
            em.getTransaction().commit();
            b=true;
        }catch (Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }
    
}
